﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using MathematicallySafe.HeadCounter.Services.Calculators;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Models;
using MathematicallySafe.HeadCounter.Services;
using StructureMap;

namespace MathematicallySafe.HeadCounter.ViewModels
{
    public class RaidLootViewModel: ViewModelBase
    {
        private static readonly IGuild Guild = ObjectFactory.GetInstance<IGuild>();

        public RaidLoot Model { get; private set; }
        private Raider _winningRaider;

        public int ID
        {
            get { return Model.ID; }
        }

        public LootItem Item
        {
            get { return Model.Item; }
        }

        public string ItemName
        {
            get { return Model.ItemName; }
        }

        public LootOutcome Outcome
        {
            get { return Model.Outcome; }
            set
            {
                Model.Outcome = value;
                OnPropertyChanged("Outcome");
            }
        }

        public string Winner
        {
            get { return Model.Winner; }
            set
            {
                Model.SetWinner(value);
                SetWinningRaider();

                OnPropertyChanged("Winner");
                OnPropertyChanged("WinningRaider");
                OnPropertyChanged("WinningIdentity");
            }
        }

        private void SetWinningRaider()
        {
            if (Model.Outcome == LootOutcome.Won)
            {
                _winningRaider = Guild.Raiders.First(r => r.Identity == Model.WinnerIdentity);
            }
            else
            {
                _winningRaider = null;
            }
        }

        public Raider WinningRaider
        {
            get { return _winningRaider; }
        }

        public string WinnerIdentity
        {
            get { return Model.WinnerIdentity; }
        }

        public string LootValueCalulation
        {
            get
            {
                return Item.ValueCalculationDetails(_winningRaider);
            }
        }
        
        public RaidLootViewModel(RaidLoot model)
        {
            Model = model;
            SetWinningRaider();
            Initialise();
        }

        protected override void SubscribeEvents()
        {
            Model.WinnerChanged += LootWinnerChanged;
            Model.OutcomeChanged += LootOutcomeChanged;
        }

        protected override void UnsubscribeEvents()
        {
            Model.WinnerChanged -= LootWinnerChanged;
            Model.OutcomeChanged -= LootOutcomeChanged;
        }

        void LootOutcomeChanged(object sender, OutcomeChangedEventArgs e)
        {
            OnPropertyChanged("Outcome");
        }

        void LootWinnerChanged(object sender, WinnerChangedEventArgs e)
        {
            OnPropertyChanged("Winner");
            OnPropertyChanged("WinnerIdentity");
        }
    }
}
